"""
Case Type   : 兼容M*数据库to_seconds函数
Case Name   : to_second函数入参为timestamp类型
Create At   : 2023.02.06
Owner       : z@wan005
Description :
    1、入参为timestamp
    2、入参为time类型不加time
    3、显式转换timestamp格式
    4、入参超范围
    5、非法时间
    6、清理环境
Expect      :
    1、成功，当小数超过6位，四舍五入
    2、报错
    3、成功
    4、报错
    5、报错
    6、成功
History     :modified by z@wan005,2023.02.13，修改用例,因入参为time类型，日期会被
             设置为当前日期，导致返回的秒数结果不固定
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest05(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = "db_to_second_0007"

    def test_time(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.log.info('----前置条件：创建兼容b库---')
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility 'b';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res)

        text = "--step1:入参为timestamp;expect:成功--"
        self.log.info(text)
        sql_cmd = "select to_seconds(timestamp'2023-2-13 14:25:59');" \
                  "select to_seconds('2023-2-13 14:25:59'::timestamp);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 9, '行数不到9行')
        self.assertEqual(2, sql_res.count('63843517559'), '执行失败' + text)

        text = "--step2:入参为time类型不加time;expect:报错--"
        self.log.info(text)
        sql_cmd = "select to_seconds('-838:59:59');" \
                  "select to_seconds(-8385959);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertEqual(2, sql_res.count('out of range'), '执行失败' + text)

        text = "--step3:显式转换timestamp格式;expect:成功--"
        self.log.info(text)
        sql_cmd = "select to_seconds(cast(20230213142559 as timestamp));"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 4, '行数不到4行')
        self.assertEqual('63843517559', sql_res.splitlines()[2].strip(),
                         '执行失败' + text)

        text = "--step4:入参超范围;expect:报错--"
        self.log.info(text)
        sql_cmd = "select to_seconds(time'-838:59:59.9');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertIn('ERROR:  truncated incorrect time value', sql_res,
                      '执行失败' + text)

        text = "--step5:非法时间;expect:报错--"
        self.log.info(text)
        sql_cmd = "select to_seconds(time'-83:60:59');" \
                  "select to_seconds(time'-83:59:60');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertEqual(2, sql_res.count(
            'ERROR:  date/time field value out of range'), '执行失败' + text)

    def tearDown(self):
        text = '----step6:清理环境;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop database {self.db_name};"
        self.log.info(sql_cmd)
        drop_res1 = self.sh_primary.execut_db_sql(sql_cmd,
                                                  dbname='postgres')
        self.log.info(drop_res1)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_res1,
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
